"""
Case Type   : cm_ctl
Case Name   : 资源池化集群执行cm_ctl pause/resume后,cm_ctl -D重启主节点
Create At   : 2024/04/05
Owner       : @kyeleze
Description :
    1.执行cm_ctl pause
    2.cm_ctl stop 停止主节点
    3.查询集群状态
    4.cm_ctl start启动主节点
    5.恢复集群
Expect      :
    1.成功
    2.成功
    3.成功
    4.成功
    5.成功
History     :
    Modified by kyleze at 2025/3/10:增加多次判断，保证查询集群状态正常
"""

import os
import unittest
import time

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro

primary_sh = CommonSH('PrimaryDbUser')


@unittest.skipIf(3 != primary_sh.get_node_num(), '非1+2环境不执行')
class CmPauseResumeCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.constant = Constant()
        self.primary_sh = CommonSH('PrimaryDbUser')

    def test_cm(self):
        step = '--step1:执行cm_ctl pause expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('pause')
        self.log.info(result)
        self.assertIn("The cluster has been paused.", result,
                      "执行失败:" + step)

        step = '--step2:cm_ctl stop 停止主节点 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl(
            'stop', f'-n 1 -I 6001')
        self.log.info(result)
        self.assertIn("stop resource instance successfully", result,
                      "执行失败:" + step)

        step = '--step3:查询集群状态 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('query', '-Cvidpw')
        self.log.info(result)
        self.assertNotIn(self.constant.CLUSTER_NORMAL_MSG, result,
                      "执行失败:" + step)
        self.assertIn("pausing         : Yes", result,
                      "执行失败:" + step)

        step = '--step4:cm_ctl start启动主节点 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl(
            'start', f'-n 1 -I 6001')
        self.log.info(result)
        self.assertIn("start resource instance successfully", result,
                         "执行失败:" + step)

        step = '--step5:恢复集群 expect:成功--'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl('resume')
        self.log.info(result)
        self.assertIn("The cluster has been resumed.", result,
                      "执行失败:" + step)
        time.sleep(10)
        result = self.primary_sh.exec_cm_ctl('query', '-Cvidpw')
        self.log.info(result)
        self.assertIn(self.constant.CLUSTER_NORMAL_MSG, result,
                         "执行失败:" + step)
        result = self.primary_sh.exec_cm_ctl('switchover', '-a')
        self.log.info(result)
        self.assertIn(self.constant.cm_switchover_success_msg, result,
                         "执行失败:" + step)

    def tearDown(self):
        step = '----恢复环境----'
        self.log.info(step)
        result = self.primary_sh.exec_cm_ctl("resume")
        self.log.info(result)
        self.assertIn("The cluster has been resumed.", result,
                      "执行失败:" + step)
        self.log.info(f'----{os.path.basename(__file__)} end----')
